package com.huawei.hwclouds.lubanops.demo.order.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huawei.hwclouds.lubanops.demo.common.biz.PageList;
import com.huawei.hwclouds.lubanops.demo.common.json.JsonUtil;
import com.huawei.hwclouds.lubanops.demo.common.model.ErrorMessage;
import com.huawei.hwclouds.lubanops.demo.order.biz.model.OrdersModel;
import com.huawei.hwclouds.lubanops.demo.order.biz.param.OrdersParam;
import com.huawei.hwclouds.lubanops.demo.order.biz.service.OrdersService;
import com.huawei.hwclouds.lubanops.demo.product.biz.api.ProductService;
import com.huawei.hwclouds.lubanops.demo.product.client.ProductServiceClient;

@RestController
@RequestMapping(value = "/apm2/demo/order/", produces = "application/json; charset=utf-8")
public class OrderController {
    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    private ExecutorService executorService = Executors.newCachedThreadPool();

    @Autowired
    private OrdersService ordersService;

    @DubboReference
    private ProductService productService;

    @PostMapping
    public PageList<OrdersModel> queryUserOrders(@RequestBody OrdersParam param) {
        return ordersService.pageQuery(param);
    }

    @PostMapping("/add")
    public ErrorMessage addOrder(@RequestBody List<OrdersModel> refModels) {
        String orderNo = UUID.randomUUID().toString();
        short tag = 0;
        refModels.forEach(model -> {
            if (model.getMerchandiseId() == null) {
                return;
            }
            model.setOrderNo(orderNo);
            model.setShipments(tag);
            model.setPay(tag);
            model.setReceipt(tag);
            model.setGmtCreate(new Date());
            model.setGmtModify(new Date());
            ordersService.create(model);
        });
        Map<String, Integer> param = new HashMap<>();
        param.put("merchandiseId", refModels.get(0).getMerchandiseId());
        param.put("count", refModels.size());
        productService.queryProducts(1);
        Future<?> future = executorService.submit(() -> ProductServiceClient.reduceRepertory(JsonUtil.toJSONString(param)));
        if (future == null) {
            logger.error("future is null");
        }
        ErrorMessage errorMessage = new ErrorMessage();
        errorMessage.setErrorMsg("执行成功");
        errorMessage.setHint("无");
        return errorMessage;
    }

    @GetMapping("/delete/{userId}")
    public ErrorMessage deleteOrder(@PathVariable("userId") String userId) {
        ordersService.deleteByUserId(userId);
        ErrorMessage errorMessage = new ErrorMessage();
        errorMessage.setErrorMsg("执行成功");
        errorMessage.setHint("无");
        return errorMessage;
    }

    @GetMapping("/clear")
    public ErrorMessage clear() {
        ordersService.clear();
        ErrorMessage errorMessage = new ErrorMessage();
        errorMessage.setErrorMsg("执行成功");
        errorMessage.setHint("无");
        return errorMessage;
    }

    @GetMapping("/product/{id}")
    public String test(@PathVariable("id") int id) {
        return JsonUtil.toJSONString(productService.queryProducts(id));
    }

}
